local jd = Package:new("jd_old")

jd.extensionName = "Super_Skeld"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["jd"] = "Skeld",
  ["sk"] = "Skeld",
  ["sk_n"] = "Re:Skeld",
  ["sk_d"] = "Skeld异画",
  ["jd_old"] = "Skeld 牢武将",
  ["Super_Skeld_old"] = "Skeld 劳武将",
}



local sk__er = General(jd, "sk__er", "skeld", 2 ,3 ,General.Male)
sk__er.shield = 2




local shangfeng = fk.CreateActiveSkill{
  name = "shangfeng",
  anim_type = "drawcard",
  min_card_num = 1,
  target_num = 0,
  prompt = "#shangfeng",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected)
    return not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local hand = player:getCardIds(Player.Hand)
    local more = #hand > 0
    for _, id in ipairs(hand) do
      if not table.contains(effect.cards, id) then
        more = false
        break
      end
    end
    room:throwCard(effect.cards, self.name, player, player)
    if not player.dead then
      room:drawCards(player, #effect.cards +1, self.name)
      if player:getMark("@shangfeng_count") <=4 then
        room:addPlayerMark(player, "@shangfeng_count")
      end
      room:setPlayerMark(player, "@pofang_pf", player:getMark("@pofang_pf")-#effect.cards*player:getMark("@shangfeng_count"))
    end
  end
}
local jinyan = fk.CreateActiveSkill{
  name = "jinyan",
  anim_type = "control",
  prompt = "#jinyan-active",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return
      #selected == 0 and
      to_select ~= Self.id and
      Self:getMark("jinyan_target") ~= to_select and
      Self:getMark("jinyan_target-turn") ~= to_select
  end,
  target_num = 1,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    if player:getMark("jinyan_target-turn") ~= 0 then
      room:setPlayerMark(player, "jinyan_target-turn", 0)
    end
    room:setPlayerMark(player, "jinyan_target", target.id)
    room:setPlayerMark(target, "@@jinyan", 1)
    room:setPlayerMark(target, "jinyan_source", effect.from)
    if target:getMark("@jinyan_count") <3 then
      room:setPlayerMark(target,"@jinyan_count",target:getMark("@jinyan_count")+1)
    end
    player:drawCards(target:getMark("@jinyan_count"), self.name)
    room:addPlayerMark(target, "jinyan_tmpbazhen")
    room:setPlayerMark(player, "@pofang_pf", player:getMark("@pofang_pf")-target:getMark("@jinyan_count")*3)
    if not target:hasSkill("bazhen", true) then
      room:handleAddLoseSkills(target, "bazhen", nil, true, false)
    end
  end,
}
local jinyanRefresh = fk.CreateTriggerSkill{
  name = "#jinyan_refresh",
  refresh_events = { fk.TurnStart, fk.AfterTurnEnd },
  can_refresh = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and player:getMark("jinyan_target") ~= 0
    end

    return
      target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      local jinyanTarget = player:getMark("jinyan_target")
      room:setPlayerMark(player, "jinyan_target", 0) 
      room:setPlayerMark(player, "jinyan_target-turn", jinyanTarget)
    else
      for _, markName in ipairs({"jinyan_tmpbazhen"}) do
        if player:getMark(markName) ~= 0 then
          room:setPlayerMark(player, markName, 0)
          room:handleAddLoseSkills(target, "-bazhen", nil, true, false)
        end
      end
    end
  end,
}
local jinyan_trigger = fk.CreateTriggerSkill{
  name = "#jinyan_trigger",
  events = {fk.FinishJudge, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(jinyan) then
      if event == fk.FinishJudge then
        return not target.dead and table.contains({"bazhen", "eight_diagram"}, data.reason) and
        target:getMark("jinyan_source") == player.id
      elseif event == fk.TurnStart then
        if target == player then
          for _, p in ipairs(player.room.alive_players) do
            if p:getMark("jinyan_source") == player.id then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = {}
    if event == fk.TurnStart then
      tos = table.filter(room.alive_players, function (p) return p:getMark("jinyan_source") == player.id end)
    else
      table.insert(tos, target)
    end
    room:doIndicate(player.id, table.map(tos, Util.IdMapper))
    for _, to in ipairs(tos) do
      if player.dead then break end
      room:handleAddLoseSkills(player, "-bazhen")
      room:setPlayerMark(to, "jinyan_source", 0)
      room:setPlayerMark(to, "@@jinyan", 0)
      if to:getMark("jinyan_tmpbazhen") > 0 then
        room:handleAddLoseSkills(to, "-bazhen", nil, true, false)
      end
      if not to:isAllNude() then
        local card = room:askForCardChosen(player, to, "hej", jinyan.name)
        room:obtainCard(player.id, card, false, fk.ReasonPrey)
      end
    end
  end,
}
local jinyan_invalidity = fk.CreateInvaliditySkill {
  name = "#jinyan_invalidity",
  invalidity_func = function(self, from, skill)
    if from:getMark("@@jinyan") > 0 then
      return not (table.contains({Skill.Wake}, skill.frequency) or
        not skill:isPlayerSkill(from) or skill.trueName == "bazhen")
    end
  end
}
jinyan:addRelatedSkill(jinyanRefresh)
jinyan:addRelatedSkill(jinyan_trigger)
jinyan:addRelatedSkill(jinyan_invalidity)



local sk__zhuiji = fk.CreateTriggerSkill{
  name = "sk__zhuiji",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:getMark("@sk__zhuiji") > 0
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#sk__zhuiji-invoke::"..data.to.id..":"..player:getMark("@sk__zhuiji"))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player.room:removePlayerMark(player, "@sk__zhuiji", 1)
    data.damage = data.damage + 1
    room:setPlayerMark(player, "@pofang_pf", player:getMark("@pofang_pf")-6)
  end,

  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    return target == player and data == self
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "@sk__zhuiji", event == fk.EventLoseSkill and 0 or (player:getLostHp() + 1))
  end,
}
local pofang = fk.CreateTriggerSkill{
  name = "pofang",
  anim_type = "support",
  events = {fk.GameStart, fk.Damaged, fk.Damage, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player:getMark("@pofang_pf") > 50 then
      return false
    end

    if event == fk.AfterCardsMove then
      return table.find(data, function(move) return move.to == player.id and move.toArea == Card.PlayerHand end)
    elseif event == fk.Damaged or event == fk.Damage then
      return target == player
    end

    return event == fk.GameStart
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = 0
    if event == fk.TurnStart then
      num = 99
    elseif event == fk.Damaged then
      num = 5 * data.damage
    elseif event == fk.AfterCardsMove then
      num = 4
    end

    local pf = player:getMark("@pofang_pf")
    num = math.min(50 - pf, num)
    if num > 0 then
      room:setPlayerMark(player, "@pofang_pf", pf + num)
      if player:getMark("@pofang_pf") >= 13 and not player:hasSkill("jinyan") then
        room:handleAddLoseSkills(player, "jinyan")
      end
      if player:getMark("@pofang_pf") >= 25 and not player:hasSkill("shangfeng") then
        room:handleAddLoseSkills(player, "shangfeng")
      end
      if player:getMark("@pofang_pf") >= 37 and not player:hasSkill("sk__zhuiji") then
        room:handleAddLoseSkills(player, "sk__zhuiji")
      end
      if player:getMark("@pofang_pf") >= 50 and not player:hasSkill("shengjing") then
        room:handleAddLoseSkills(player, "shengjing")
      end
    end
  end,

  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    return target == player and data == self
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventAcquireSkill then
      if player:getMark("@pofang_pf") >= 13 and not player:hasSkill("jinyan") then
        room:handleAddLoseSkills(player, "jinyan")
      end
      if player:getMark("@pofang_pf") >= 25 and not player:hasSkill("shangfeng") then
        room:handleAddLoseSkills(player, "shangfeng")
      end
      if player:getMark("@pofang_pf") >= 37 and not player:hasSkill("sk__zhuiji") then
        room:handleAddLoseSkills(player, "sk__zhuiji")
      end
      if player:getMark("@pofang_pf") >= 50 and not player:hasSkill("shengjing") then
        room:handleAddLoseSkills(player, "shengjing")
      end
    else
      room:setPlayerMark(player, "@pofang_pf", 0)
      local toLose = {
        "-jinyan",
        "-shangfeng",
        "-sk__zhuiji",
        "-shengjing",
      }
      room:handleAddLoseSkills(player, table.concat(toLose, "|"))
    end
  end,
}
local shengjing = fk.CreateActiveSkill{
  name = "shengjing",
  anim_type = "offensive",
  frequency = Skill.Limited,
  card_num = 0,
  target_num = 0,
  prompt = function(self, card)
    return "#shengjing-active"
  end,
  can_use = function(self, player)
    return player:getHandcardNum() > 0 and player:getMark("@pofang_pf") == 50 and
      table.find(player:getCardIds("h"), function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    if player.general == "sk__er" then
      player.general = "sk_d__er"
      room:broadcastProperty(player, "general")
    end
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p.dead then
        room:loseHp(p, 1, self.name)
      end
    end
    room:setPlayerMark(player, "@pofang_pf", player:getMark("@pofang_pf")-13)
  end,
}
shengjing.permanent_skill = true
pofang.permanent_skill = true
sk__er:addRelatedSkill(jinyan)
sk__er:addRelatedSkill(shengjing)
sk__er:addRelatedSkill(shangfeng)
sk__er:addRelatedSkill(sk__zhuiji)
sk__er:addSkill(pofang)



Fk:loadTranslationTable{
  ["sk__er"] = "ER",
  ["#sk__er"] = "万恶之源",
  ["designer:sk__er"] = "塔罗牌",
  ["cv:sk__er"] = "ER",
  ["illustrator:sk__er"] = "ER",
  ["qianlong"] = "破防",
  ["jinyan"] = "禁言",
  [":jinyan"] = "出牌阶段限一次，你可令一名其他角色（不能连续两回合选择同一名角色）的所有技能替换为〖八阵〗（觉醒技除外），然后你摸X张牌并减少3X点破防值（X为本局游戏禁言其的次数且至多为3）。"..
  "你的回合开始时或当其【八卦阵】判定后，你令其失去〖八阵〗并获得原技能，然后你获得其区域里的一张牌。",
  ["pofang"] = "破防",
  [":pofang"] = "持恒技;以下情况时，你可以骂一句“傻鸟”然后无事发生：游戏开始时，摸牌阶段开始时，造成伤害时;<br>当你受到1点伤害后,获得5点破防值;当你于摸牌阶段外获得手牌时，获得4点破防值；回合开始时，获得5点破防值。" ..
  "<br>你根据破防值视为拥有以下技能：13点-〖禁言〗；25点-〖上风〗；37点-〖追杀〗；" ..
  "50点-〖圣经〗。你的破防值上限为50。",
  ["@pofang_pf"] = "破防值",
  ["$pofang"] = "傻鸟！",
  ["~sk__er"] = "玩元神玩多了...",
  ["shengjing"] = "圣经",
  [":shengjing"] = "出牌阶段，若你的破防值为50，你可以失去13点破防值，然后令所有其他角色各失去1点体力。",
  ["#shengjing-active"] = "圣经：失去13点破防值，然后令所有其他角色各失去1点体力。",
  ["sk_d_er"] = "ER",
  ["$shengjing1"] = "就我这个群主看不顺眼是吧，行 太对啦太对啦我经典破防我碰到你们哪里了我又经典破防,天天好踢好死，牛逼666，差不多得了",
  ["$shengjing2"] = "你真以为制裁别人好玩？还在报私仇？我一踢某个人就要来炸我，真以为自己是圣母？站在道德的最高点？",
  ["$shengjing3"] = "我是***什么的不过也是那几句了老掉牙了都，自己喜欢就进吧，炸吧，来来来，就我是错的，你们都是对的hhhh真棒真棒 你们做的太对辣 炸我房间太对辣",
  ["$shengjing4"] = "在别的群骂我太对辣 啊对对对 啊对对对 你们真是圣母 啊 我的缺点全被你们找到辣 你们太棒辣 我破防 我报私仇 我不喜欢别人拿我头像做事 啊对对对 真的太牛逼辣 太棒辣",
  ["sk__zhuiji"] = "追杀",
  [":sk__zhuiji"] = "当你造成伤害时，每局游戏限X次，你可以使此伤害加1并减少6点破防值（X为获得此技能时已损体力值+1）",
  ["shangfeng"] = "上风",
  [":shangfeng"] = "出牌阶段，你可以弃置任意张手牌，失去X点破防值，然后你摸等量牌，额外摸一张牌（X为弃置牌数*Y，Y为本局游戏上风发动次数且至多为5）<br>此技能名取自典故《别以为自己占上风》",
  ["#shangfeng"] = "弃置任意张手牌，然后你摸等量牌加1",
  ["@sk__zhuiji"] = "追杀",
  ["#sk__zhuiji-invoke"] = "是否追加伤害",
  ["$jinyan"] = "玩原神！",
  ["$shangfeng"] = "肯定是玩原神玩多了",
  ["$sk__zhuiji"] = "傻鸟原神玩多了",
  ["$shengjing"] = "傻鸟傻鸟傻鸟傻鸟傻鸟",
  ["jinyan_use"] = "使用乐不思蜀",
  ["#jinyan_use"] = "使用乐不思蜀",
  ["@@jinyan"] = "已禁言",
  ["#jinyan-active"] = "禁言一名角色",
  ["#jinyan_trigger"] = "禁言,举办<br>拟态北鼻辣",
  ["@jinyan_count"] = "禁言:",
  ["@shangfeng_count"] = "上风:",
}






return jd